<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model as BaseModel;
use DateTimeInterface;
use DateTime;

/**
 * @mixin \Illuminate\Database\Eloquent\Builder
 * @method static void insert(array $values)
 * @method static static where(string $column, $operator = null, $value = null, $boolean = 'and')
 * @method static static whereDate(string $column, $operator, $value = null, $boolean = 'and')
 * @method static static whereBetween(string $column, array $values, $boolean = 'and', $not = false)
 */
class Model extends BaseModel
{

    const CREATED_AT = 'create_time';

    const UPDATED_AT = 'update_time';

    /**
     * 为数组 / JSON 序列化准备日期。
     *
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    /**
     * @param $raw_time
     * @return string
     * @throws \Exception
     */
    public function getCreateTimeAttribute($raw_time): string
    {
        return $this->dateTimeFormat($raw_time);
    }

    /**
     * 格式化更新时间
     * @param $raw_time
     * @return string
     * @throws \Exception
     */
    public function getUpdateTimeAttribute($raw_time): string
    {
        return $this->dateTimeFormat($raw_time);
    }

    /**
     * 格式化时间
     * @param $raw_time
     * @param string $format
     * @return string
     * @throws \Exception
     */
    public function dateTimeFormat($raw_time, string $format = 'Y-m-d H:i:s'): string
    {
        return (new DateTime($raw_time))->format($format);
    }


}
